import sys
import os
sys.path.append(os.getcwd())
from enum import Enum
import datetime
from util.Encoder import*
import util.Encoder
import util.tool
from util.test1 import getConfig
import pprint
from dateutil.relativedelta import relativedelta
import os
os.environ['MONO'] = '/usr/bin/mono' 
import clr

from util.DLT698 import *
#from util.GeneralProcedure import*

#from src.mode.detection.server import PlatformDevice, SmartEnergyUnit

import sys
sys.path.append(os.getcwd())
clr.AddReference('System')
clr.AddReference('dll/DLT69845')  # 加载DLL
from DLT69845 import *

def getExistMeterNo():
    return "567800000001"

# def test():
# #def test(platform: PlatformDevice, exec_notify, y_esam_no, energy_unit, address):
#     #
#     self = None

#     addr = getExistMeterNo()
    

#     am = Apdu_Proxy("000000000011", False)

#     year = int(datetime.datetime.now().strftime("%Y"))
#     mon = int(datetime.datetime.now().strftime("%m"))
#     day = int(datetime.datetime.now().strftime("%d"))
#     hour = int(datetime.datetime.now().strftime("%H"))
#     mins = int(datetime.datetime.now().strftime("%M"))
#     sec = int(datetime.datetime.now().strftime("%S"))
    


#     ProxySetList0 = ProxySetList()
#     ProxySetList0.svr_addr = TSA(addr)
#     ProxySetList0.svr_timeout = DT_UInt16(0)

#     ProxySetItem0 = ProxySetItem()
#     ProxySetItem0.oad = OAD("40000200")
#     ProxySetItem0.data = VarData(DT_DateTime_S(year,mon,day,hour,mins,sec))
#     ProxySetList0.proxySetItem = [ProxySetItem0]
    
#     overtime = DT_UInt16(30)
#     proxySetLists = [ProxySetList0]

#     am.ProxySetRequestList(overtime, proxySetLists)
#     print("apdu:",(am.get_Apdu_Str()))
#     apdu = am.get_Apdu_Str()
#     #apdu = self.Send_fr(frame_send,"PROXY_01， 测试设备向IUT发出代理请求", 1)


#     apdu = "89 03 20 01 07 05 00 00 00 00 00 01 01 40 00 02 00 00 00 00".replace(" ","")
#     apdu_byte = bytearray(tool.apdustrTointarray(apdu))
#     ProxySetResponseList = ProxyResponse.Parse(apdu_byte,0,len(apdu_byte))
#     try:
#         #print("lens:",(ProxyGetResponseList.ProxygetResult[0].svr_addr))
#         #print("lens:",(ProxyGetResponseList.ProxygetResult[0].proxyGetAResult.Length))
#         dar = ProxySetResponseList.target_ProxySetResult[0].a_ProxySetResult[0].dar
#         if dar == 0:
#             #Printer("ROXY_05，测试失败",None,self,False)
#             Printer("ROXY_10，测试成功",None,self,True)
#         else:
#             Printer("ROXY_10，测试失败",None,self,False)
        
#     except Exception as e:
#         #self.append_step("PROXY_01，测试成功测试失败,e:" + str(e))
#         #self.Is_qualified = 0
#         #print("PROXY_03，测试成功测试失败,e:" + str(e))
#         #self.append_step("PROXY_03，测试失败")
#         #self.Is_qualified = 0
#         Printer("ROXY_10，测试失败",str(e),self,False)


# def Printer(msg:str, data,gp:GeneralProcedure, succ:bool):
#     if not gp:
#         if not data:
#            print(msg)
#         else:
#             print(msg,data)
        
#     else:
#         gp.append_step(msg)
#         if succ != True:
#             gp.Is_qualified = 0



def Make_A_MeterDoc_Basic_obj(addr:str, bautrate:int, protoType:int, comOad:str, commuPsw:str, rateCnt:int, userType:int, lineType:int, vol:int, cur:int):
    dt_struct =  DT_Struct()
    dt_struct.Add(TSA(addr))
    dt_struct.Add(DT_Enum(bautrate.to_bytes(1, 'big')))
    dt_struct.Add(DT_Enum(protoType.to_bytes(1, 'big')))
    dt_struct.Add(OAD(comOad))
    dt_struct.Add(DT_OString(commuPsw))
    dt_struct.Add(DT_UInt8(rateCnt.to_bytes(1, 'big')))
    dt_struct.Add(DT_UInt8(userType.to_bytes(1, 'big')))
    dt_struct.Add(DT_Enum(lineType.to_bytes(1, 'big')))
    dt_struct.Add(DT_UInt16(vol))
    dt_struct.Add(DT_UInt16(cur))
    return dt_struct

def Make_A_MeterDoc_Extended_obj(collAddr:str, assertNum:str,pt:int,ct:int):
    dt_struct =  DT_Struct()
    dt_struct.Add(TSA(collAddr))
    dt_struct.Add(DT_OString(assertNum))
    dt_struct.Add(DT_UInt16(pt))
    dt_struct.Add(DT_UInt16(ct))
    return dt_struct

def Make_A_MeterDoc_Annex_obj(oad:str = None, data:VarData = None):
    arr = DT_Array()
    return arr

def Make_A_MeterDoc(cfgNum:int, b_obj:DT_Struct, e_obj:DT_Struct, annex_obj:DT_Array):
    dt_struct =  DT_Struct()
    dt_struct.Add(DT_UInt16(cfgNum))
    dt_struct.Add(b_obj)
    dt_struct.Add(e_obj)
    dt_struct.Add(annex_obj)
    return dt_struct

def Make_MeterDoc_Array(docs:list):
    arr = DT_Array()
    for doc in docs:
        arr.Add(doc)
    return arr


def GetApdu(meter, is_, datas):
    apdu = Apdu_Action(meter, is_)
    apdu.ActionRequest(0x60008000, VarData(datas))
    return apdu.get_Apdu_Str()
def get_set_meter_docs_apdu():
    meterAddrs = ["000000000001","000000000002","000000000003"]
    docs = []
    for i,addr in enumerate(meterAddrs):
        #cfgNum = DT_UInt16(i)
        Basic_obj = Make_A_MeterDoc_Basic_obj(addr,3,3,"F2010201","000000000000",4,1,3,2200,15)
        Extended_obj = Make_A_MeterDoc_Extended_obj("000000000000", "000000000000",1,1)
        Annex_obj = Make_A_MeterDoc_Annex_obj()

        meterDoc = Make_A_MeterDoc(i + 1, Basic_obj, Extended_obj, Annex_obj)
        docs.append(meterDoc)
    
    datas =  Make_MeterDoc_Array(docs)
    apdu = Apdu_Action("000000000006", False)
    apdu.ActionRequest(0x60008000, VarData(datas))
    return apdu.get_Apdu_Str()



if __name__ == "__main__":
    #test()
    apdu = get_set_meter_docs_apdu()
    print("apdu:",apdu)
        
